package web

import (
	"jgt-oper/src/bll"
	"jgt-oper/src/context"
	"jgt-oper/src/schema"
	"jgt-oper/src/util"
)

// LogManage 日志管理
type LogManage struct {
	LogManageBll *bll.LogManage `inject:""`
}

// QueryPage 查询分页数据
func (a *LogManage) QueryPage(ctx *context.Context) error {
	pageIndex, pageSize := ctx.GetPageIndex(), ctx.GetPageSize()

	var params schema.LogManageQueryParam

	params.Type = ctx.Query("type")
	params.System = ctx.Query("system")
	params.TraceID = ctx.Query("trace_id")
	params.UserID = ctx.Query("user_id")
	params.StartTime = util.S(ctx.Query("start_time")).Int64()
	params.EndTime = util.S(ctx.Query("end_time")).Int64()
	if v := ctx.Query("level"); v == "" {
		params.Level = -1
	} else {
		params.Level = util.S(v).Int()
	}

	total, items, err := a.LogManageBll.QueryPage(params, pageIndex, pageSize)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	result := map[string]interface{}{
		"list": items,
		"pagination": map[string]interface{}{
			"total":    total,
			"current":  pageIndex,
			"pageSize": pageSize,
		},
	}

	return ctx.ResSuccess(result)
}
